Skip to content

Add fake support for turn on/off for Apple TV#5962

Merged
balloob merged 3 commits into
home-assistant:devfrom
postlund:atv_power
Feb 15, 2017
Merged

Add fake support for turn on/off for Apple TV#5962
balloob merged 3 commits into
home-assistant:devfrom
postlund:atv_power

Conversation

@postlund
Copy link
Copy Markdown
Contributor

Description:
Some users have reported that their Apple TVs automatically start up when Home Assistant start. This is mostly a problem when CEC is enabled since it starts other devices as well. Since I have not yet found a way to know if a device is in standby or not, this cannot easily be fixed. With this PR however, it is possible to "fake" turn off/on a device so that no requests are sent to it. This way the main issue no longer exists, but the user must "turn on" the device again to get updates in Home Assistant. This is probably as good as it gets for now.

There's also a config option so that a device can start in off mode (this must be set to True). Documentation will be written when a user has verified functionality.

Related issue (if applicable): fixes #5917

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>

Example entry for configuration.yaml (if applicable):

media_player:
  - platform: apple_tv
    name: Apple TV
    host: 10.0.10.26
    login_id: 00000000-000-000-0000-00000000000
    start_off: True

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

When the device is "turned off", no requests are sent to the device.
When the setting "start_off" is set to true, the device starts in off
state.
@mention-bot
Copy link
Copy Markdown

@postlund, thanks for your PR! By analyzing the history of the files in this pull request, we identified @balloob and @armills to be potential reviewers.

_LOGGER.error('failed to connect to Apple TV (%s)', str(ex))
except asyncio.TimeoutError:
_LOGGER.warning('timed out while connecting to Apple TV')
if not self._is_off:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a guard clause:

if self._is_off:
    return

It will save you from indentation hell 👍

"""Turn the media player on."""
self._is_off = False
self._reset()
self.schedule_update_ha_state()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to call this. Your device does not overwrite should_poll to return False, so Home Assistant is polling this device, including polling it right after it calls a service.

return new_playing.media_type != old_playing.media_type or \
new_playing.title != old_playing.title

def turn_on(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this only sets simple variables, please turn this into it's async variant async_turn_off. Has to be a coroutine. Make sure to also rename _reset to be called _async_reset and make it a @callback (imported from ha.core)

self._reset()
self.schedule_update_ha_state()

def turn_off(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

SUPPORT_PLAY_MEDIA
else:
return SUPPORT_PLAY_MEDIA
features |= SUPPORT_PLAY_MEDIA
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move SUPPORT_PLAY_MEDIA to be always part of features and return when _playing is None or state == STATE_IDLE

@balloob balloob merged commit fdb6dd8 into home-assistant:dev Feb 15, 2017
aronsky pushed a commit to aronsky/home-assistant that referenced this pull request Feb 15, 2017
* Add fake support for turn on/off for Apple TV

When the device is "turned off", no requests are sent to the device.
When the setting "start_off" is set to true, the device starts in off
state.

* Fix async comments

* Clean up supported features
@home-assistant home-assistant locked and limited conversation to collaborators May 19, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apple TV randomly turning on

4 participants